﻿using System;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Web;
using System.Xml;
using System.Collections.Generic;

namespace NewsExtractor
{
    public class HttpUtil
    {
        static public string getPage(string URL)
        {
            //Get remote response
            string lcUrl = URL;
            // *** Establish the request 
            HttpWebRequest loHttp =
                 (HttpWebRequest)WebRequest.Create(lcUrl);
            loHttp.Method = "GET";
            loHttp.ContentType = "application/x-www-form-urlencoded";

            // *** Set properties
            loHttp.Timeout = 10000;     // 10 secs
            loHttp.UserAgent = "Requet Remote Data";
            // *** Retrieve request info headers
            HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
            Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page
            StreamReader loResponseStream =
               new StreamReader(loWebResponse.GetResponseStream(), enc);
            string lcHtmlXML = loResponseStream.ReadToEnd();
            loWebResponse.Close();
            loResponseStream.Close();

            return lcHtmlXML;
        }

        static public List<BaiBao> getListBaiBaoFromRSS(string RSS_content)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(new StringReader(RSS_content));
            XmlElement xroot = xdoc.DocumentElement;
            XmlNodeList x_item_tag = xroot.SelectNodes("//item");

            List<BaiBao> listBaiBao = new List<BaiBao>();

            foreach (XmlNode curItem in x_item_tag)
            {
                BaiBao curBaiBao = new BaiBao();
                curBaiBao.TieuDe = curItem["title"].InnerText;
                curBaiBao.MoTa = curItem["description"].InnerText;
                curBaiBao.URL = curItem["link"].InnerText;
                curBaiBao.NoiDung = getPage(curBaiBao.URL);
                listBaiBao.Add(curBaiBao);
            }
            return listBaiBao;
        }
    }
}
